package org.unicat.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.unicat.mapper.UserInfoMapper;
import org.unicat.mapper.UserMapper;
import org.unicat.model.Student;
import org.unicat.model.Teacher;
import org.unicat.model.User;
import org.unicat.model.UserInfo;
import org.unicat.service.StudentService;
import org.unicat.service.TeacherService;
import org.unicat.service.UserRegistService;
import org.unicat.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserRegistService userRegistService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    //返回首页
    @RequestMapping("/index")
    public String index(Model model) {
        return "adminManage";
    }

    //查找所有学生
    @RequestMapping("/studentQuery")
    public String findAllStudent(Model model) {
        List<Student> students = studentService.findAllStudent();

        //测试
//        for (Student student : students) {
//            System.out.println(student);
//        }

        model.addAttribute("students", students);
        return "adminFindStudent";
    }

    //按名称查找学生
    @RequestMapping("/studentQueryByName")
    public String findStudentByName(Model model, UserInfo userInfo) {

        List<Student> students = new ArrayList<>();
        students.add(studentService.findStudentByName(userInfo.getUsername()));

//        System.out.println(students);

        model.addAttribute("students", students);
        return "adminFindStudent";
    }

    //添加学生
    @RequestMapping("/addStudent")
    public String addStudent(Model model, UserInfo studentInfo) {
        studentService.addStudent(studentInfo);
        return "adminAddStudent";
    }

    //查询学生个人信息
    @RequestMapping("/studentInfo")
    public String studentInfo(Model model, String name) {
        Student student = new Student();
        student.setName(name);

        UserInfo studentInfo = studentService.getStudentInfo(student);

        //测试
//        System.out.println(tearcherInfo);

        model.addAttribute("studentInfo", studentInfo);
        return "adminStudentInfo";
    }

    //查找所有学生
    @RequestMapping("/teacherQuery")
    public String teacherQuery(Model model) {
        List<Teacher> teachers = teacherService.findAllTeacher();

        //测试
//        for (Student student : students) {
//            System.out.println(student);
//        }

        model.addAttribute("teachers", teachers);
        return "adminFindTeacher";
    }


    //按名称查找老师
    @RequestMapping("/teacherQueryByName")
    public String teacherQueryByName(Model model, UserInfo userInfo) {

        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacherService.findTeacherByName(userInfo.getUsername()));

//        System.out.println(teachers);

        model.addAttribute("teachers", teachers);
        return "adminFindTeacher";
    }

    //添加老师
    @RequestMapping("/addTeacher")
    public String addTeacher(Model model, UserInfo teacherInfo) {
        teacherService.addTeacher(teacherInfo);
        return "adminAddTeacher";
    }

    //获取老师信息
    @RequestMapping("/teacherInfo")
    public String teacherInfo(Model model, String name) {
        Teacher teacher = new Teacher();
        teacher.setName(name);

        UserInfo teacherInfo = teacherService.getTearcherInfo(teacher);

        //测试
//        System.out.println(tearcherInfo);

        model.addAttribute("teacherInfo", teacherInfo);
        return "adminTeacherInfo";
    }

    //更新信息，包括学生、老师和管理员的
    @RequestMapping("/updateInfo")
    public String updateInfo(Model model, UserInfo userInfo) {
        //先判断身份
        String identity = userInfo.getIdentity();
        switch (identity) {
            case "student":
                studentService.updateStudentInfo(userInfo);
                return "redirect:/admin/studentQuery";

            case "teacher":
                teacherService.updateTeacherInfo(userInfo);
                return "redirect:/admin/teacherQuery";

            case "admin":
                //更新User登录表信息
                userService.updateUser(userInfo);
                //更新个人简介
                userService.updateUserInfo(userInfo);
                break;

            default:
                break;
        }
        return "redirect:/admin/index";
    }


    @RequestMapping("/deleteUser")
    public String deleteUser(Model model, String username) {

        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setIdentity(userInfoMapper.selectOne(username).getIdentity());

        //测试
//        System.out.println(userInfo);

        //先判断身份
        switch (userInfo.getIdentity()) {
            case "student":
                Student student = new Student();
                student.setName(userInfo.getUsername());
                student.setMajor(userInfo.getMajor());
                studentService.deleteStudent(student);
                return "redirect:/admin/studentQuery";

            case "teacher":
                Teacher teacher = new Teacher();
                teacher.setName(userInfo.getUsername());
                teacher.setMajor(userInfo.getMajor());
                teacherService.deleteTeacher(teacher);
                return "redirect:/admin/teacherQuery";

            case "admin":
                //删除User登录表信息
                userService.deleteUser(userInfo);
                //删除个人简介
                userService.deleteUserInfo(userInfo);
                return "redirect:/admin/index";

            default:
                return "redirect:/deleteStudentByNamePage";
        }

    }
}
